package com.woniuxy.mesboot.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.mesboot.dto.ProInspDTO;
import com.woniuxy.mesboot.entity.InspectionRecord;
import com.woniuxy.mesboot.entity.MesProductInspection;
import com.woniuxy.mesboot.service.ProductInspectionService;
import com.woniuxy.mesboot.util.Result;
import com.woniuxy.mesboot.vo.ProInspVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("proinsp")
public class ProductInspectionController {
    @Resource
    private ProductInspectionService productInspectionService;

    @PostMapping("queryAll")
    public Result queryAll(@RequestBody ProInspDTO inspDTO) {
        PageHelper.startPage(inspDTO.getCurrentPage(),inspDTO.getPageSize(),true);
        List<ProInspVO> i = productInspectionService.selectAll(inspDTO);
        PageInfo<ProInspVO> pageInfo = new PageInfo<>(i);
        return Result.ok().data("list", i).data("total",pageInfo.getTotal());
    }

    @PostMapping
    public Result insert(@RequestBody MesProductInspection record) {
        int insert = productInspectionService.insertSelective(record);
        if (insert > 0) {
            return Result.ok("添加成功");
        } else {
            return Result.error("添加失败");
        }
    }

    @DeleteMapping("{inspectionProductId}")
    public Result delete(@PathVariable("inspectionProductId") Long id) {
        int i = productInspectionService.deleteByPrimaryKey(id);
        if (i > 0) {
            return Result.ok("删除成功");
        } else {
            return Result.error("删除失败");
        }
    }

    @GetMapping("{inspectionProductId}")
    public Result queryById(@PathVariable("inspectionProductId") Long id) {
        MesProductInspection productInspection = productInspectionService.selectByPrimaryKey(id);
        return Result.ok().data("product", productInspection);
    }

    @PutMapping
    public Result update(@RequestBody MesProductInspection record) {
        int i = productInspectionService.updateByPrimaryKey(record);
        if (i > 0) {
            return Result.ok("修改成功");

        } else {
            return Result.error("修改失败");
        }
    }
}
